package Server.Poker.HnNiuNiu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import org.json.JSONObject;

import Server.Agent.GameConfig;
import Server.Agent.GameDB;
import Server.Model.Base.IRoom;
import Server.Model.Base.IServerUserItem;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import activity.ActivityMgr;
import activity.UserActionActivity;
import club.ClubRedis;
import rank.RankMgr;
import redis.protocal.DbInfoMgr;
import redis.protocal.UserRedisInfo;

public class niuniuTable extends IRoom {
	@Override
	public int GetID() {
		return m_dwTableID;
	};

	@Override
	public int GetCreator() {
		return m_Creator;
	}
	@Override
	public String GetType() {
		return "HNNIUNIU";
	};
	@Override
	public void OnClientClose(int uid) {
		OnUserStandUp(uid,false);
	};

	public boolean SendData(int wChairID, String sData) {
		// 群发用户
		if (wChairID == Constants.INVALID_CHAIR) {
			for (short i = 0; i < m_pIUserItem.length; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {
						// mPlayers[i].SendGameMessage(wSubCmdID, pData,
						// wDataSize, data);
					}
				}
			}

			return true;
		} else {
			if (wChairID >= m_pIUserItem.length) {
				dthall.getInstance().onSend(wChairID, sData);
			} else if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {
				}
				return true;
			}
		}

		return false;
	}

	int m_dwLastDoCmdTime;
	public int fangzhu;
	public int CanSit(int _Uid) {

		if(this.isDwcType()){
			UserRedisInfo info = dthall.getInstance().getSafeUserRedisInfo(_Uid);
			if(info.json.optInt("userRmbType") == 1 && !canUserJoinRentRoom(info.json.optInt("upAgentUid"))){
				return 22;
			}
		}
		if (m_Creator == _Uid) {
			return 14;
		}
		if(m_bTableStarted)
			return 12;
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			if (m_payWay.equalsIgnoreCase("FZPAY")) {
				if(_Uid ==fangzhu){
					if (user.getGem(0) < m_byCostGem * 5) {
						return 11;
					}
				}
			
			}else{
				if (m_payWay.equalsIgnoreCase("AAPAY") )
				{
					if (user.getGem(0) < m_byCostGem) {
						return 11;
					}
				}
			}
		}

		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				return i;
			}
		}
		if (m_locked != 0) {
			return 13;//已经开始 并且房主不让近
		}	
		
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		if (cntUser >= 5) {
			return 13;
		}
		
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				return i;
			}
		}

		return 12;
	};

	Long m_lStartTime;
	long needDealTime;
	int m_dwTableID;
	long m_dismissTimer;
	int maxPeople = 5;
	int m_byAgreeDismiss[] = new int[5];

	boolean m_bGameStarted;
	boolean m_bTableStarted;
	int m_byRound;
	int m_byMaxRound;
	int m_wFaPaiDelay; // 发牌延迟
	int m_wRobotSeconds[] = new int[5]; // 等待时长
	int m_nextUser = 0;//轮庄时的下一个用户


	int m_byQiangZhuang[] = new int[5]; // 是否抢庄，和抢庄的倍率
	int m_byXiaZhu[] = new int[5]; // 每个人的下注
	int m_byXuanNiu[] = new int[5]; // 每个人的下注

	int m_wZhuangPower;// 当前庄的倍率
	
	public int m_byCostGem;
	public int m_fdFen;
	public String m_payWay;
	public String m_betMode;	//经典倍数、345倍、疯狂10倍
	public String m_xianshiMode;//明牌模式、暗牌模式
	public String m_zhuangMode;//选庄，抢庄，轮庄
	public String m_guize;	   //规则
	
	public String m_strParam;
	public String m_strRoomDesc;
	
	public String[] m_vecParam;
	
	public int m_byZhuang;// 当前庄位置
	public int m_vecWinScore[] = new int[5];
	public int m_vecLoseScore[] = new int[5];
	public int m_vecCardType[] = new int[5];
	public int m_vecRoundScore[] = new int[5];
	public int m_vecResultScore[] = new int[5];

	GameLogic m_GameLogic = new GameLogic();
	String m_curStage;
	int m_bTableCardArray[][] = new int[5][5]; // 桌面扑克 //been 5
	
	int m_locked = 0;//默认允许进
	
	IServerUserItem m_pIUserItem[] = new IServerUserItem[5]; // 用户指针
	{
		for (int i = 0; i < m_pIUserItem.length; i++) {
			m_pIUserItem[i] = new IServerUserItem();
		}
	}
	long m_LookOnUserID[] = new long[50]; // 观众

	public int m_Creator;// 房主
	niuniuTable() {
		m_bTableStarted = false;
		m_lStartTime = System.currentTimeMillis() / 1000;
		m_bRemove = false;
		m_byCostGem = 0;
		m_fdFen = 0;
		
		m_payWay = "FZPAY";
		m_betMode = "BETMODE0";
		
		m_xianshiMode = "MINGPAI";
		m_zhuangMode = "QIANGZHUANG";
		m_guize = "GUIZE_DEFAULT";
		
		m_strParam = "";
		m_strRoomDesc = "";
		
	}

	void Init(int cbReason) {

		m_dwLastDoCmdTime = 0;
		m_bGameStarted = false;
		m_wFaPaiDelay = 2;

		for (int i = 0; i < 5; i++) {
			m_wRobotSeconds[i] = 0;

			m_byQiangZhuang[i] = 0;
			m_vecCardType[i] = 0;
			m_byXiaZhu[i] = 0;
			m_byXuanNiu[i] = 0;
			m_wZhuangPower = 1;
			

			m_vecRoundScore[i] = 0;

		}
		switch (cbReason) {
		case Constants.INIT_TOTAL: {
			
			m_dismissTimer = 0;
			m_byAgreeDismiss[0] = 0;
			m_byAgreeDismiss[1] = 0;
			m_byAgreeDismiss[2] = 0;
			m_byAgreeDismiss[3] = 0;
			m_byAgreeDismiss[4] = 0;
			Constants.memset(m_LookOnUserID, 0, Constants.sizeof(m_LookOnUserID)); //
			m_byRound = 0;
			m_byMaxRound = 0;
			m_byZhuang = 0;
			for (int i = 0; i < 5; i++) {
				m_vecWinScore[i] = 0;
				m_vecLoseScore[i] = 0;
				m_vecResultScore[i] = 0;

			}
		}
			break;
		case Constants.INIT_FIRST_ROUND: {

		}
			break;
		case Constants.INIT_NEWROUND: {

		}
			break;
		}
	}

	void KickRobots() {
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
	}

	boolean HasRobot() {
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				return true;
			}
		}
		return false;
	}

	void SendAvatar(int _Uid) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}

		String iter = (dthall.getInstance()).m_mapAvatar.get(_Uid);

		SUBCMD_S_Avatar cmd = new SUBCMD_S_Avatar();
		cmd.m_lUid = _Uid;
		if (iter != null) {
			cmd.szAvatarURL = iter;
		} else {
			cmd.szAvatarURL = "http://img.cjpoker.com/uumj.png";
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	public void OnUserSit(int _Uid) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					// m_pIUserItem[i].lUserScore = user.getCoin();
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					// Constants.memset(&cmd, 0, Constants.sizeof(cmd));

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);

					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					SendAvatar(m_pIUserItem[i].dwUserID);

					return;
				}
			}
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID == 0) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					m_pIUserItem[i].lUserScore = 0;
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					// Constants.memset(&cmd, 0, Constants.sizeof(cmd));

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);

					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					return;
				}
			}
		}

		return;
	}

	int FindSeat(long _Uid) {
		for (int bySeatID = 0; bySeatID < 5; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	int GetPlayerCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	int GetRobotCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	public void OnUserStandUp(int _Uid,boolean leaveroom) {
		int wChairID = FindSeat(_Uid);
		
		if(m_bTableStarted == false && wChairID != 0 && leaveroom == true){
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}

		if (m_bTableStarted == false && leaveroom == true) {
			if (wChairID > 0 && wChairID < 5) {
				m_pIUserItem[wChairID].Init();
				QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			} else {
			}
		} else {
			if (wChairID < 5) {
				m_pIUserItem[wChairID].cbOnline = Constants.US_OFFLINE;
			}
		}
	}
	public void SetParam(String szParam) {
		m_strParam = szParam;
		
		m_vecParam = m_strParam.split(";");
	}

	public void SetDesc(String szDesc) {
		m_strRoomDesc = szDesc;

	}
	boolean SendGameScene(int _Uid, int bGameStatus) {
		int wChairID = FindSeat(_Uid);
		if(wChairID >= 5)
		{
			return false;
		}
		if (m_strParam.length() > 0) {
			String tmpRoomDesc = m_strRoomDesc;
			
			SUBCMD_S_Param cmd = new SUBCMD_S_Param();
			cmd.m_dwTableID = m_dwTableID;
			cmd.szParam = m_strParam;
			cmd.szRoomDesc = tmpRoomDesc;
			SendData(_Uid, cmd.getJsonData());
		}		
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}

		{
			SUBCMD_S_Table cmd = new SUBCMD_S_Table();
			cmd.m_dwTableID = m_dwTableID;// 房间ID
			cmd.m_bGameStarted = m_bGameStarted ? 1 : 0;// 是否开始
			cmd.m_bTableStarted = m_bTableStarted ? 1 : 0;// 是否开始
			cmd.m_byRound = m_byRound;
			cmd.m_byMaxRound = m_byMaxRound;
			cmd.m_cntUser = cntUser;// 玩家
			cmd.m_cntLookon = 1;// 观众
			Constants.memset(cmd.m_seatInfo, 0, Constants.sizeof(cmd.m_seatInfo));

			for (int i = 0; i < 5; i++) {
				cmd.m_seatInfo[i].m_lUid = m_pIUserItem[i].dwUserID;// UID
				cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserScore;// COIN
				cmd.m_seatInfo[i].m_NickName = m_pIUserItem[i].szNickName;// NICKNAME
				cmd.m_seatInfo[i].m_iBetCoin = 0;//
				cmd.m_seatInfo[i].m_cbGender = m_pIUserItem[i].cbGender;
				cmd.m_seatInfo[i].m_iAvatar = m_pIUserItem[i].dwFaceID;//
				cmd.m_seatInfo[i].m_cbUserStatus = m_pIUserItem[i].cbUserStatus;// 用户状态
				cmd.m_seatInfo[i].m_iVipLevel = m_pIUserItem[i].cbVipLevel;// VIP
																			// LEVEL
				cmd.m_seatInfo[i].m_iTitle = 0;// 称号
				cmd.m_seatInfo[i].m_isLookon = 0;// 是否观众
				cmd.m_seatInfo[i].szIP = m_pIUserItem[i].szIP;// NICKNAME
			}

			SendData(_Uid, cmd.getJsonData());
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SendAvatar(m_pIUserItem[i].dwUserID);
				}
			}
		}

		if (m_bTableStarted == true) {
			SUBCMD_S_STAGE cmd = new SUBCMD_S_STAGE();

			cmd.szStage = m_curStage;
			cmd.m_byZhuang = m_byZhuang;// 当前庄位置
			cmd.m_wZhuangPower = m_wZhuangPower;// 当前庄的倍率
			cmd.needDealTime = (int) needDealTime;
			for (int i = 0; i < 5; i++) {
				cmd.m_byQiangZhuang[i] = m_byQiangZhuang[i]; // 是否抢庄，和抢庄的倍率
				cmd.m_byXiaZhu[i] = m_byXiaZhu[i]; // 每个人的下注
				cmd.m_byXuanNiu[i] = m_byXuanNiu[i]; // 每个人的下注

				cmd.m_vecCardType[i] = m_vecCardType[i];
				cmd.m_vecRoundScore[i] = m_vecRoundScore[i];
			}
			for (int k = 0; k < 5; k++) {
				cmd.m_bTableCardArray[wChairID][k] = m_bTableCardArray[wChairID][k]; // 桌面扑克
			}
			SendData(_Uid, cmd.getJsonData());			
			
		}

		switch (bGameStatus) {
		case Constants.GS_FREE: // 空闲状态
		{
			// 构造数据

			return true;
		}
		case Constants.GS_PLAYING: // 游戏状态
		{
			// 构造数据

			return true;
		}
		}

		return true;
	}
	int random(int iMin, int iMax){
		Random ra =new Random();
		return ra.nextInt(iMax-iMin) + iMin;
	}
	
	void FaPai_1() {
		m_bTableStarted = true;
		m_bGameStarted = true;

		m_curStage = "STAGE_FAPAI_1";

		if(m_zhuangMode.equalsIgnoreCase("LUNZHUANG") || m_zhuangMode.equalsIgnoreCase("DINGZHUANG"))
		{
			needDealTime = 2;	
		}else{
			needDealTime = 6;	
		}
		


		if (m_byRound == 0) {
			m_byRound = 1;
		}
		
//		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,	//方块 A - K
//		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,	//梅花 A - K
//		0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,	//红桃 A - K
//		0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,	//黑桃 A - K
		
		int m_bTableCardArray1[] = new int[5*5];
		m_GameLogic.RandCardList(m_bTableCardArray1,m_bTableCardArray1.length ); //计算数组的个数
	
		 //int m_bTableCardArray1[] = {0x3A, 0x3B, 0x3C, 0x3D,0x31,0x01,0x11,0x21,0x12,0x02,0x04,0x14,0x24,0x06,0x16,10,18,19,20,21,22,23,24,25};
		 
		 for (int i = 0; i < m_bTableCardArray1.length; i++) {
			int x = i /5;
			int y = i %5;
			m_bTableCardArray[x][y] = m_bTableCardArray1[i];
		}
		 for(int i=0;i<4;i++)
		 {
			 if(m_pIUserItem[i].dwUserID == 1003)
			 {
				 if(random(0,12) == 7)
				 {
					 int byDaPai = m_GameLogic.GetDaPai(m_bTableCardArray[i]);
					 if(byDaPai > 0)
					 {
						 m_bTableCardArray[i][4] = byDaPai;
						 break;
					 }
				 }
			 }
		 }
	

		SUBCMD_S_FaPai_1 cmd = new SUBCMD_S_FaPai_1();
		cmd.byRound = m_byRound;
		cmd.byMaxRound = m_byMaxRound;
		
		for (int i = 0; i < 5; i++) {
			m_vecRoundScore[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0) {

				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				cmd.vecIsReady[i] = 0;
			}
		}

		if(m_xianshiMode.equalsIgnoreCase("MINGPAI"))
		{
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == Constants.US_PLAY) {
					for (int k = 0; k < 4; k++) {
						cmd.byPai[k] = m_bTableCardArray[i][k];
					}
					SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
				}
			}
		}
		if(m_xianshiMode.equalsIgnoreCase("ANPAI"))
		{
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == Constants.US_PLAY) {
					for (int k = 0; k < 5; k++) {
						cmd.byPai[k] = 0;
					}
					SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
				}
			}
		}
	}

	void FaPai_2() {
		m_curStage = "STAGE_FAPAI_2";
		needDealTime = 3;

		SUBCMD_S_FaPai_2 cmd = new SUBCMD_S_FaPai_2();
		for (int i = 0; i < 5; i++) {
			m_vecRoundScore[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0) {

				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				cmd.vecIsReady[i] = 0;
			}
		}

		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == Constants.US_PLAY) {

				cmd.byPai = m_bTableCardArray[i][4];
				for (int j = 0; j < 5; j++) {
					cmd.byHandPai[j] = m_bTableCardArray[i][j];
				}
				SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());

			}
		}
	}

	void OnEventGameStart() {
		Init(Constants.INIT_NEWROUND);

		FaPai_1();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
			}
		}
		m_bTableStarted = true;
		m_bGameStarted = true;
	}

	void OnEventGameEnd(int bySeatID, int cbReason) {
		m_byRound = m_byRound + 1;
		OnRoundInfoChange();
		m_curStage = "STAGE_JIESUAN";
		needDealTime = 1;

		m_bGameStarted = false;
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		if (cbReason != Constants.GAME_END_DISMISS) {


			{

				SUBCMD_S_JieSuan cmd = new SUBCMD_S_JieSuan();

				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0) {
						int byOutCardData[] = new int[5];
						m_vecCardType[i] = m_GameLogic.GetCardType(m_bTableCardArray[i], 5, byOutCardData);
						// 牌型
						cmd.bCardType[i] = m_vecCardType[i];
						// 牌内容
						cmd.bCardData[i][0] = byOutCardData[0];
						cmd.bCardData[i][1] = byOutCardData[1];
						cmd.bCardData[i][2] = byOutCardData[2];
						cmd.bCardData[i][3] = byOutCardData[3];
						cmd.bCardData[i][4] = byOutCardData[4];
					} else {
						int byOutCardData[] = new int[5];
						cmd.bCardType[i] = 0;
						cmd.bCardData[i][0] = byOutCardData[0];
						cmd.bCardData[i][1] = byOutCardData[1];
						cmd.bCardData[i][2] = byOutCardData[2];
						cmd.bCardData[i][3] = byOutCardData[3];
						cmd.bCardData[i][4] = byOutCardData[4];
					}

				}
				int byPowerRate1[] = { 0, 1, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 0, 0, 0, 0, 5, 5, 5,5,5,5,5,5 }; //经典倍数
				int byPowerRate2[] = { 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 15, 16, 17 ,18,19,20,25,25};//疯狂加倍
				
				int byPowerRate[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
				if(m_betMode.equalsIgnoreCase("BETMODE0"))
				{
					Constants.CopyMemory(byPowerRate,byPowerRate1,25);
				}
				if(m_betMode.equalsIgnoreCase("BETMODE1"))
				{
					Constants.CopyMemory(byPowerRate,byPowerRate2,25);
				}	
				
				// m_vecRoundScore 一局的输赢
				// m_vecResultScore 本次桌子所有局输赢
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID <= 0) {
						continue;
					}
					if (i != m_byZhuang) {
						int Multiple = 0;
						if (m_vecCardType[i] > m_vecCardType[m_byZhuang]) {
							int coin = m_byXiaZhu[i] * byPowerRate[m_vecCardType[i]] * m_wZhuangPower;

							m_vecRoundScore[m_byZhuang] -= coin;
							m_vecRoundScore[i] += coin;

							m_vecResultScore[m_byZhuang] -= coin;
							m_vecResultScore[i] += coin;
							m_pIUserItem[i].lUserScore += coin;
							m_pIUserItem[m_byZhuang].lUserScore -= coin;
						} else if (m_vecCardType[i] < m_vecCardType[m_byZhuang]) {
							int coin = m_byXiaZhu[i] * byPowerRate[m_vecCardType[m_byZhuang]] * m_wZhuangPower;

							m_vecRoundScore[m_byZhuang] += coin;
							m_vecRoundScore[i] -= coin;

							m_vecResultScore[m_byZhuang] += coin;
							m_vecResultScore[i] -= coin;

							m_pIUserItem[i].lUserScore -= coin;
							m_pIUserItem[m_byZhuang].lUserScore += coin;
						} else {
							if (m_GameLogic.CompareCard(m_bTableCardArray[m_byZhuang], 5, m_bTableCardArray[i], 5,
									Multiple) == 1) {// Next 大 庄输
								int coin = m_byXiaZhu[i] * byPowerRate[m_vecCardType[i]] * m_wZhuangPower;

								m_vecRoundScore[m_byZhuang] -= coin;
								m_vecRoundScore[i] += coin;

								m_vecResultScore[m_byZhuang] -= coin;
								m_vecResultScore[i] += coin;
								m_pIUserItem[i].lUserScore += coin;
								m_pIUserItem[m_byZhuang].lUserScore -= coin;
							} else {
								int coin = m_byXiaZhu[i] * byPowerRate[m_vecCardType[m_byZhuang]] * m_wZhuangPower;

								m_vecRoundScore[m_byZhuang] += coin;
								m_vecRoundScore[i] -= coin;

								m_vecResultScore[m_byZhuang] += coin;
								m_vecResultScore[i] -= coin;

								m_pIUserItem[i].lUserScore -= coin;
								m_pIUserItem[m_byZhuang].lUserScore += coin;
							}
						}
					} else {

					}
				}

				for (int i = 0; i < 5; i++) {
					cmd.nRoundScore[i] = m_vecRoundScore[i];
					cmd.nUserScore[i] = m_pIUserItem[i].lUserScore;
				}
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

				for (int i = 0; i < 5; i++) {// 清除牌局数据
					m_byQiangZhuang[i] = 0;
					m_byXiaZhu[i] = 0;
					m_byXuanNiu[i] = 0;
					m_vecRoundScore[i] = 0;
 				}
				m_wZhuangPower = 1;
				if(m_zhuangMode.equalsIgnoreCase("QIANGZHUANG"))
				{
					m_byZhuang = 0;	
				}
				

			}

		}

		if (m_byRound <= 1 && cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

		} else if (m_byRound > m_byMaxRound || cbReason == Constants.GAME_END_DISMISS) {
			int before_cost[] = new int[5];
			int after_cost[] = new int[5];

			int cost[] = new int[5];			
			for(int i=0;i<5;i++)
			{
				before_cost[i] = 0;
				cost[i] = 0;
				after_cost[i] = 0;
			}
			
			for (int i = 0; i < 5; i++) 
			{				
				if(m_pIUserItem[i].dwUserID > 0)
				{
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null)
					{
						before_cost[i] = user.getGem(0);
					}
				}
			}			
			int minRound = 1;
			if(m_byMaxRound > 8)
			{
				minRound = 4;
			}			
			if (m_byRound > minRound) {

				if (m_payWay.equalsIgnoreCase("FZPAY")) {
					int cost_gem = 0;
					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}
					if (m_Creator > 0) {
						SHMUser user = dthall.getInstance().getUserInfo(m_Creator);
						if (user != null && cost_gem < 100) {
							user.getGem(0 - cost_gem);
						}
					} else {
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[0].dwUserID);
						if (user != null && cost_gem < 100) {
							user.getGem(0 - cost_gem);
						}
						cost[0] = 0 - cost_gem;
					}
				}
				if (m_payWay == "AAPAY") {
					for (int i = 0; i < 5; i++) {
						int lFangZhu = m_pIUserItem[i].dwUserID;
						if (lFangZhu > 0) {
							SHMUser user = dthall.getInstance().getUserInfo(lFangZhu);
							if (user != null && m_byCostGem < 20) {
								user.getGem(0 - m_byCostGem);
								cost[i] = 0 - m_byCostGem;
							}
						}
					}
				}
				if (m_payWay.equalsIgnoreCase("CLUBPAY")) {
					int cost_gem = 0;
					for (int i = 0; i < 4; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}
					DbInfoMgr.getInstance().costClubOwnerGem(this.m_clubOwnerId,cost_gem);
				}
				for (int i = 0; i < 5; i++) 
				{				
					if(m_pIUserItem[i].dwUserID > 0)
					{
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
						if(user != null)
						{
							after_cost[i] = user.getGem(0);
						}
					}
				}					
			}
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = 0;

			for (int k = 0; k < 5; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			log_counter++;
			log_counter = log_counter % 100;
			Long t = System.currentTimeMillis() / 1000;
			Long battle_id = (t % 1000000) * 100 + log_counter;

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String created = df.format(new Date());
			{

				JSONObject root = new JSONObject();

				root.put("command", "save_battle_log");
				root.put("battle_id", battle_id);

				root.put("room_type", GetType());

				root.put("room_id", m_dwTableID);
				root.put("creator", GetCreator());
				
				root.put("jushu", m_byMaxRound);
				root.put("limitscore", 0);
				root.put("difen", 0);
				
				if (m_payWay == "WINPAY") {
					root.put("payway", 1);	
				}else if (m_payWay == "BIGWINPAY") {
					root.put("payway", 2);
				}else if(m_payWay == "AAPAY"){
					root.put("payway", 0);
				}else{
					root.put("payway", 10);
				}
				root.put("created", created);
				root.put("uid0", (int) m_pIUserItem[0].dwUserID);
				root.put("nickname0", m_pIUserItem[0].szNickName);
				root.put("score0", m_vecResultScore[0]);
				root.put("cost0", cost[0]);
				root.put("uid1", (int) m_pIUserItem[1].dwUserID);
				root.put("nickname1", m_pIUserItem[1].szNickName);
				root.put("score1", m_vecResultScore[1]);
				root.put("cost1", cost[1]);
				root.put("uid2", (int) m_pIUserItem[2].dwUserID);
				root.put("nickname2", m_pIUserItem[2].szNickName);
				root.put("score2", m_vecResultScore[2]);
				root.put("cost2", cost[2]);
				root.put("uid3", (int) m_pIUserItem[3].dwUserID);
				root.put("nickname3", m_pIUserItem[3].szNickName);
				root.put("score3", m_vecResultScore[3]);
				root.put("cost3", cost[3]);
				root.put("uid4", (int) m_pIUserItem[4].dwUserID);
				root.put("nickname4", m_pIUserItem[4].szNickName);
				root.put("score4", m_vecResultScore[4]);
				root.put("cost4", cost[4]);
				
				root.put("headurl0", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[0].dwUserID));
				root.put("headurl1", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[1].dwUserID));
				root.put("headurl2", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[2].dwUserID));
				root.put("headurl3", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[3].dwUserID));
				root.put("headurl4", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[4].dwUserID));
				
				if(m_clubId != 0){
					//club
					root.put("clubid", m_clubId);
					root.put("wfdesc", m_wanFaStr);
					if(m_payWay == "CLUBPAY"){
						root.put("payway", 10);
					}
					ClubRedis.getInstance().addClubBattleLog(m_clubId,root.toString());
				}
				
				GameDB.getInstance().push_task(null, root);

				try
				{
					for(int i =0;i<5;i++)
					{
						if(m_pIUserItem[i].dwUserID > 0 && cost[i] != 0)
						{
							JSONObject root_cost = new JSONObject();
							root_cost.put("command", "save_charge_log");
							root_cost.put("created", created);
							root_cost.put("uid", (int) m_pIUserItem[i].dwUserID);
							root_cost.put("before_cost", (int) before_cost[i]);
							root_cost.put("after_cost", (int) after_cost[i]);
							root_cost.put("cost", (int) cost[i]);
							root_cost.put("reason", "用户" + m_pIUserItem[0].dwUserID + "创建   牛牛  " + m_byMaxRound + "局 " + m_payWay );
							root_cost.put("room_id", m_dwTableID);
							
							GameDB.getInstance().push_task(null, root_cost);
						}
					}	
				}catch(Exception e)
				{
					e.printStackTrace();
				}
			}

			m_bTableStarted = false;
			for (int i = 0; i < 5; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);
			
			//处理积分
			if(m_byRound == m_byMaxRound){
				int score = 1;
				for(int i = 0;i < 4;i++){
					if(m_pIUserItem[i].dwUserID > 0){
						int uid = m_pIUserItem[i].dwUserID;
						UserActionActivity activity = ActivityMgr.getInstance().handleSendHuaFeiActionByPlay(uid,score,"*");
						if(activity.m_useActioned){
							RankMgr.getInstance().addUserRankScore(uid,activity.m_expGet);
						}
					}
				}
			}
		}

	}

	static int log_counter = 0;


	void OnDealQiangZhuang() {

		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = 8;

		List<Integer> vecQiangZhuang = new ArrayList<>();
		List<Integer> vecTmpUser = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_byQiangZhuang[i] > 0) {
					vecQiangZhuang.add(i);
				}
				vecTmpUser.add(i);
			}
		}
		int byQiangZhuang[] = new int[5];
		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;
		if (vecQiangZhuang.size() > 0) {
			int byZhuang = vecQiangZhuang.get(Constants.rand() % vecQiangZhuang.size());
			if (m_byQiangZhuang[byZhuang] == 0) {
				m_byQiangZhuang[byZhuang] = 1;
			}
			cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
			cmd.wChairID = byZhuang;
			cmd.wPower = m_byQiangZhuang[byZhuang];

			for (int i = 0; i < vecQiangZhuang.size(); i++) {
				byQiangZhuang[vecQiangZhuang.get(i)] = m_byQiangZhuang[vecQiangZhuang.get(i)];
			}
			m_byZhuang = byZhuang;
		} else {
			if (vecTmpUser.size() > 0) {
				int byZhuang = vecTmpUser.get(Constants.rand() % vecTmpUser.size());
				if (m_byQiangZhuang[byZhuang] == 0) {
					m_byQiangZhuang[byZhuang] = 1;
				}
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0) {
					} else {
						m_byQiangZhuang[i] = 0;
					}
				}
				cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
				cmd.wChairID = byZhuang;
				cmd.wPower = m_byQiangZhuang[byZhuang];

				for (int i = 0; i < vecTmpUser.size(); i++) {
					byQiangZhuang[vecTmpUser.get(i)] = m_byQiangZhuang[vecTmpUser.get(i)];
					if (byQiangZhuang[vecTmpUser.get(i)] < 0 || byQiangZhuang[vecTmpUser.get(i)] > 250) {
						byQiangZhuang[vecTmpUser.get(i)] = 1;
					}
				}
				m_byZhuang = byZhuang;
			} else {
				return;
			}

		}
		for (int i = 0; i < 5; i++) {
			cmd.byQiangZhuang[i] = byQiangZhuang[i];
		}
		m_wZhuangPower = m_byQiangZhuang[m_byZhuang];
		m_wZhuangPower = 1;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	void QiangZhuang(int byZhuang)
	{
		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = 8;
		
		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;
		
		if (m_byQiangZhuang[byZhuang] == 0) {
			m_byQiangZhuang[byZhuang] = 1;
		}
		cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
		cmd.wChairID = byZhuang;
		cmd.wPower = m_byQiangZhuang[byZhuang];

		m_byZhuang = byZhuang;		

		cmd.byQiangZhuang[byZhuang] = 1;
		
		m_wZhuangPower = 1;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());		
	}
	void OnDealXiaZhu() {
		m_curStage = "STAGE_XIAZHU";
		needDealTime = 1;

		int default_xiazhu = 1;
//		if(m_betMode.equalsIgnoreCase("BETMODE1"))
//		{
//			default_xiazhu = 3;
//		}
//		if(m_betMode.equalsIgnoreCase("BETMODE2"))
//		{
//			default_xiazhu = 10;
//		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_byZhuang != i) {
				if (m_byXiaZhu[i] == 0) {
					
					SUBCMD_S_XiaZhu cmd = new SUBCMD_S_XiaZhu();
					cmd.lUid = m_pIUserItem[i].dwUserID;
					cmd.wChairID = i;
					
					if(i==m_byZhuang){
						cmd.wPower = 0;
					}else{
						m_byXiaZhu[i] = default_xiazhu;
						cmd.wPower = default_xiazhu;
					}
					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
				}
			}
		}

	}

	int GetNextUser(int bySeatID)
	{
		int wNextUser = 0;
		for (int i = 1; i < 6; i++) {
			wNextUser = (bySeatID + i) % 5;
			if (m_pIUserItem[wNextUser].dwUserID > 0) {
				return wNextUser;
			}
			
		}
		return 0;
	}
	public  long currDealTime = 0;
	void OnDealAI(float dt) {
		m_DealTime = (int) (System.currentTimeMillis() / 1000);
		
		if(currDealTime == 0){
			currDealTime = System.currentTimeMillis();
		}else{
			long time = System.currentTimeMillis();
			if(time - currDealTime >= 1000){
				currDealTime = time;
			}else{
				return;
			}
		}
		if (m_wFaPaiDelay > 0) {
			m_wFaPaiDelay--;
			return;
		}
		int cntUser = 0;
		for (int wChairID = 1; wChairID < 5; wChairID++) {
			if (m_pIUserItem[wChairID].dwUserID > 0) {
				cntUser++;
			}
		}
		if (m_curStage == "STAGE_FAPAI_1") 
		{
			if(m_zhuangMode.equalsIgnoreCase("LUNZHUANG"))
			{
				int byNextUser = GetNextUser(m_byZhuang);
				QiangZhuang(byNextUser);
			}else if(m_zhuangMode.equalsIgnoreCase("DINGZHUANG"))
			{
				QiangZhuang(m_byZhuang);
			}else{
				OnDealQiangZhuang();
			}
		} else if (m_curStage == "STAGE_QIANGZHUANG") {
			OnDealXiaZhu();
		} else if (m_curStage == "STAGE_XIAZHU") {
			FaPai_2();
		} else if (m_curStage == "STAGE_FAPAI_2") {
			OnEventGameEnd(0, Constants.GAME_END_NORMAL);
			needDealTime = (cntUser * 3 + 5) / 2;
		}
	}
	public long currDealTime1 = 0;
	public void OnDeal(int dt) {
		if(currDealTime1 == 0){
			currDealTime1 = System.currentTimeMillis();
		}else{
			long time = System.currentTimeMillis();
			if(time - currDealTime1 >= 1000){
				currDealTime1 = time;
			}else{
				return;
			}
		}
		if (m_bTableStarted == true) {
			if (needDealTime > 0) {
				needDealTime--;
				return;
			}
			if (m_wFaPaiDelay > 0) {
				m_wFaPaiDelay--;
				return;
			}
			if (m_bGameStarted) {
				OnDealAI(dt);
			} else {
				OnEventGameStart();
			}

		}
	}

	static int randRobotIndex = 999;

	void AddRobot() {
		randRobotIndex++;
		int robot_uid = randRobotIndex % 9999 + 200000;

		Vector<Integer> vecEmpty = new Vector<>();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);

			}
		}
		if (vecEmpty.size() > 0) {
			int rand = Constants.rand(0, 10000);
			int wChairID = vecEmpty.get(Constants.rand() % vecEmpty.size());

			int vecMaleFace[] = { 1, 3, 5, 7, 9 };
			int vecFemaleFace[] = { 2, 4, 6, 8, 10 };
			m_pIUserItem[wChairID].dwUserID = robot_uid;
			m_pIUserItem[wChairID].lUserScore = 1000;
			m_pIUserItem[wChairID].wChairID = wChairID;
			m_pIUserItem[wChairID].cbGender = rand % 2;
			if (m_pIUserItem[wChairID].cbGender == 1) {
				m_pIUserItem[wChairID].dwFaceID = vecMaleFace[rand % 5];
			} else {
				m_pIUserItem[wChairID].dwFaceID = vecFemaleFace[rand % 5];
			}
			m_pIUserItem[wChairID].cbUserStatus = Constants.US_NULL;
			m_pIUserItem[wChairID].isAndroidUser = 1;
			
			m_pIUserItem[wChairID].szNickName = "USER[" + wChairID + "]";

			{
				SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
				

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;
				cmd.m_lUserScore = m_pIUserItem[wChairID].lUserScore;
				cmd.m_iAvatar = m_pIUserItem[wChairID].dwFaceID;
				cmd.szNickName = m_pIUserItem[wChairID].szNickName;
				cmd.szIP = "";

				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			{
				m_pIUserItem[wChairID].cbUserStatus = Constants.US_READY;

				SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
				

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;
				cmd.m_dwTableID = m_dwTableID;
				cmd.m_byRound = m_byRound;

				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			m_wRobotSeconds[wChairID] = 0;
		}
	}

	void Handle_onJoin(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}

		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		

		String sData = cmd.getJsonData();
		SendData(_Uid, sData);
	}

	void Handle_onReady(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntReady = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
				cntReady++;
			}
		}

		cntReady = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
				cntReady++;
			}
		}
		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = bySeatID;
		cmd.m_dwTableID = m_dwTableID;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		if (cntReady == 5) {
			OnEventGameStart();
		} else {

		}
	}

	void Handle_onRobot(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		if (cntPlayer < 5) {
			for (int i = 0; i < 5 - cntPlayer; i++) {
				AddRobot();
			}
		}
	}

	// 表情
	void Handle_onBiaoQing(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iBiaoQingIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onYuYin(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iYuYinIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 送礼
	void Handle_onSongLi(int _Uid, String vecArg[]) {
	}

	// 聊天
	void Handle_onChat(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		String strContent = vecArg[1];

		SUBCMD_S_Chat cmd = new SUBCMD_S_Chat();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onVoice(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 3)
			return;
		int seconds = Constants.atoi(vecArg[1]);
		String strVoiceURL = vecArg[2];

		SUBCMD_S_Voice cmd = new SUBCMD_S_Voice();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		cmd.szVoiceURL = strVoiceURL;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void Handle_onVoiceMsg(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		float seconds = Float.parseFloat(vecArg[1]);
		
		SUBCMD_S_VoiceMsg cmd = new SUBCMD_S_VoiceMsg();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		
		cmd.data = vecArg[2];
		cmd.m_maxClip = Integer.parseInt(vecArg[3]);
		cmd.m_currClip = Integer.parseInt(vecArg[4]);
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	// 语音
	void Handle_onTryDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		for (int i = 0; i < 5; i++)
		{
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0)
			{
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE)
				{
					cntOffline++;
				}
				else{
					cntOnline++;
				}
			}
		}		
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = Constants.time(null);
			}
			m_byAgreeDismiss[bySeatID] = 1;

			SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
			cmd.lUid = _Uid;
			cmd.dwTableID = m_dwTableID;
			cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			
			if (cntOnline == 1)
			{
				OnEventGameEnd(0, Constants.GAME_END_DISMISS);
				DelayRemove();
				QpGameMgr.getInstance().OnDismissRoom(this);

				m_dismissTimer = 0;
			}			
		} else {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
		}

	}

	// 语音
	void Handle_onAgreeDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		m_byAgreeDismiss[bySeatID] = 1;

		SUBCMD_S_AgreeDismissRoom cmd = new SUBCMD_S_AgreeDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (bySeatID == 0 && m_bTableStarted == false) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntAgree = 0;
		for (int i = 0; i < 5; i++) {
			if (m_byAgreeDismiss[i] > 0) {
				cntAgree++;
			}
		}
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		if (cntAgree >= 2 && cntAgree >= cntUser - 1) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

			if (ShouldBeRemoved()) {
				// niuniuTableMgr::instance().DismissTable(this);
			}
		}
	}

	// 语音
	void Handle_onRefuseDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;

		m_dismissTimer = 0;
		m_byAgreeDismiss[0] = 0;
		m_byAgreeDismiss[1] = 0;
		m_byAgreeDismiss[2] = 0;
		m_byAgreeDismiss[3] = 0;
		m_byAgreeDismiss[4] = 0;

		SUBCMD_S_RefuseDismissRoom cmd = new SUBCMD_S_RefuseDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void Handle_onStart(int _Uid, String vecArg[]) {
		if (vecArg.length < 2)
			return;

		int bySeatID = FindSeat(_Uid);
		if (m_bTableStarted == false && bySeatID == 0 && GetPlayerCount() > 1) {
			OnEventGameStart();
		}
	}

	void Handle_onQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;

		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}

		m_byQiangZhuang[bySeatID] = 1;

		int power = Constants.atoi(vecArg[2]);

		SUBCMD_S_QiangZhuang cmd = new SUBCMD_S_QiangZhuang();
		cmd.lUid = _Uid;
		cmd.wChairID = bySeatID;
		cmd.wPower = power;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 5; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_FAPAI_1") {
			OnDealQiangZhuang();
		}
	}

	void Handle_onBuQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		m_byQiangZhuang[bySeatID] = -1;

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 5; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_FAPAI_1") {
			needDealTime = 3;
			OnDealQiangZhuang();
		}

	}

	void Handle_onXiaZhu(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onXiaZhu Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		int nRound = Constants.atoi(vecArg[1]);
		int power = Constants.atoi(vecArg[2]);
		if (m_byXiaZhu[bySeatID] == 0) {
			m_byXiaZhu[bySeatID] = power;

			SUBCMD_S_XiaZhu cmd = new SUBCMD_S_XiaZhu();
			cmd.lUid = _Uid;
			cmd.wChairID = bySeatID;
			cmd.wPower = power;
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			int curNum = 0;
			int xiazhuN = 0;
			for (int i = 0; i < 5; i++) {
				if (m_byXiaZhu[i] > 0) {
					xiazhuN++;
				}
				if (m_pIUserItem[i].dwUserID > 0) {
					curNum++;
				}
			}
			if (xiazhuN >= curNum - 1 && m_curStage == "STAGE_QIANGZHUANG") {
				// 全部准备好发第二次牌
				needDealTime = 3;
				OnDealXiaZhu();
				FaPai_2();
			}
		}
	}

	void Handle_onXuanNiu(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		m_byXuanNiu[bySeatID] = 1;

		int curNum = 0;
		int xuanNiuN = 0;
		for (int i = 0; i < 5; i++) {
			if (m_byXuanNiu[i] > 0) {
				xuanNiuN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (xuanNiuN >= curNum && m_curStage == "STAGE_FAPAI_2") {
			// 全部准备好发第二次牌

			OnEventGameEnd(0, Constants.GAME_END_NORMAL);
			int cntUser = 0;
			for (int wChairID = 1; wChairID < 5; wChairID++) {
				if (m_pIUserItem[wChairID].dwUserID > 0) {
					cntUser++;
				}
			}
			needDealTime = (cntUser * 3 + 5) / 2;
		}
	}

	public int FindSeat(int _Uid) {
		for (int bySeatID = 0; bySeatID < 5; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	public int m_DealTime = 0;

	public int getDealTime() {
		return m_DealTime;
	}
	
	public boolean m_bRemove = false;

	public void DelayRemove() {
		m_bRemove = true;
	}

	public void ResetKillFlag() {
		m_bRemove = false;
	}

	public boolean ShouldBeRemoved() {
		return m_bRemove;
	}
	
	public void Handle_onLock(Integer lUid, String[] vecArg) {
		
		if (vecArg.length < 2)
			return;
		if ( m_bTableStarted && m_pIUserItem[0].dwUserID == lUid) {//已经开始 并且是房主
			int index = Constants.atoi(vecArg[1]);
			m_locked = index;			
			
		}
	}

	@Override
	public String getRoomInfo() {
		// TODO 自动生成的方法存根
		//m_lStartTime
		//m_pIUserItem
		// m_bTableStarted;
		// m_byRound;
		// m_byMaxRound;
		//m_dwTableID
		String roomInfo = String.format("%s/%s/%s/%s/%s", 
				m_pIUserItem[0].GetUserInfo(), //#
				m_pIUserItem[1].GetUserInfo(), 
				m_pIUserItem[2].GetUserInfo(), 
				m_pIUserItem[3].GetUserInfo(),
				m_pIUserItem[4].GetUserInfo()
				);
		
		String[] vecParam = m_strParam.split(";");
		String vecParamStr = "";
		for (int i = 0; i < vecParam.length; i++) {
			String param = vecParam[i];

			if (param.equalsIgnoreCase("8JU")) {
				vecParamStr += "8局 ";
			}
			if (param.equalsIgnoreCase("16JU")) {
				vecParamStr += "16局 ";
			}
			if (param.equalsIgnoreCase("DINGZHUANG")) {
				
				vecParamStr += "房主坐庄  ";
			}
			if (param.equalsIgnoreCase("LUNZHUANG")) {
				vecParamStr += "轮流坐庄  ";
			}
			if (param.equalsIgnoreCase("QIANGZHUANG")) {
				vecParamStr += "经典抢庄  ";
			}
			if (param.equalsIgnoreCase("BETMODE0")) {
				vecParamStr += "经典倍数 ";
			}
			if (param.equalsIgnoreCase("BETMODE1")) {
				vecParamStr += "疯狂加倍";
			}
		}
		
		int start = 0;
		if(m_bTableStarted){
			start = 1;
		}
		String descInfo = String.format("牛牛 %s",vecParamStr);
		roomInfo = String.format("%s|%d|%d|%d|%s|%d|%d|%d|%s|%s", roomInfo,m_dwTableID,m_lStartTime,start,descInfo,maxPeople,m_byRound,m_byMaxRound,"牛牛","NIUNIU");
		
		return roomInfo;
	}
	
	@Override
	public int getPlayerCount() {
		// TODO Auto-generated method stub
		return GetPlayerCount();
	}
	@Override
	public int getCostGem() {
		// TODO Auto-generated method stub
		return m_byCostGem;
	}
	@Override
	public int getStatus() {
		// TODO Auto-generated method stub
		if(m_bTableStarted == false)
		{
			return 0;
		}
		return 1;
	}

	@Override
	public String getRoomDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getCellScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getMinScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getPayway() {
		// TODO Auto-generated method stub
		return m_payWay;
	}

	@Override
	public String GetRoundInfo() {
		return "" + m_byRound + "/" +  m_byMaxRound;
	}
}
